\chapter{分支}

\section{逻辑运算符}

\subsection{关系运算符}

编程中经常需要使用关系运算符来比较两个数据的大小，比较的结果是一个布尔值（boolean），即True（非0）或False（0）。\\

在编程中需要注意，一个等号=表示赋值运算，而两个等号==表示比较运算。\\

\begin{table}[H]
	\centering
	\setlength{\tabcolsep}{5mm}{
		\begin{tabular}{|c|c|}
			\hline
			\textbf{数学符号} & \textbf{关系运算符} \\
			\hline
			$ < $             & <                   \\
			\hline
			$ > $             & >                   \\
			\hline
			$ \le $           & <=                  \\
			\hline
			$ \ge $           & >=                  \\
			\hline
			$ = $             & ==                  \\
			\hline
			$ \ne $           & !=                  \\
			\hline
		\end{tabular}
	}
\end{table}

\vspace{0.5cm}

\subsection{逻辑运算符}

逻辑运算符用于连接多个关系表达式，其结果也是一个布尔值。\\

\begin{enumerate}
	\item 逻辑与\&\&：当多个条件全部为True，结果为True。\\
	      \begin{table}[H]
		      \centering
		      \setlength{\tabcolsep}{5mm}{
			      \begin{tabular}{|c|c|c|}
				      \hline
				      \textbf{条件1} & \textbf{条件2} & \textbf{条件1 \&\& 条件2} \\
				      \hline
				      T              & T              & T                         \\
				      \hline
				      T              & F              & F                         \\
				      \hline
				      F              & T              & F                         \\
				      \hline
				      F              & F              & F                         \\
				      \hline
			      \end{tabular}
		      }
	      \end{table}

	\item 逻辑或||：多个条件至少有一个为True时，结果为True。\\
	      \begin{table}[H]
		      \centering
		      \setlength{\tabcolsep}{5mm}{
			      \begin{tabular}{|c|c|c|}
				      \hline
				      \textbf{条件1} & \textbf{条件2} & \textbf{条件1 || 条件2} \\
				      \hline
				      T              & T              & T                       \\
				      \hline
				      T              & F              & T                       \\
				      \hline
				      F              & T              & T                       \\
				      \hline
				      F              & F              & F                       \\
				      \hline
			      \end{tabular}
		      }
	      \end{table}

	\item 逻辑非!：条件为True时，结果为False；条件为False时，结果为True。\\
	      \begin{table}[H]
		      \centering
		      \setlength{\tabcolsep}{5mm}{
			      \begin{tabular}{|c|c|}
				      \hline
				      \textbf{条件} & \textbf{!条件} \\
				      \hline
				      T             & F              \\
				      \hline
				      F             & T              \\
				      \hline
			      \end{tabular}
		      }
	      \end{table}
\end{enumerate}

\newpage

\section{if}

\subsection{if}

if语句用于判断一个条件是否成立，如果成立则进入语句块，否则不执行。\\

\mybox{年龄}

\begin{lstlisting}[language=C++]
#include <iostream>

using namespace std;

int main()
{
	int age;
	cout << "Enter your age: ";
	cin >> age;
	if(age > 0 && age < 18)
	{
		cout << "Minor" << endl;
	}
	return 0;
}
\end{lstlisting}

\begin{tcolorbox}
	\mybox{运行结果}
	\begin{verbatim}
Enter your age: 17
Minor
\end{verbatim}
\end{tcolorbox}

\vspace{0.5cm}

\subsection{if-else}

if-else的结构与if类似，只是在if语句块中的条件不成立时，执行else语句块中的语句。\\

\mybox{闰年}

\begin{lstlisting}[language=C++]
#include <iostream>

using namespace std;

int main()
{
	int year;
	cout << "Enter a year: ";
	cin >> year;

	/*
		* A year is a leap year if it is
		* 1. exactly divisible by 4, and not divisible by 100;
		* 2. or is exactly divisible by 400
		*/
	if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
	{
		cout << "Leap year" << endl;
	}
	else
	{
		cout << "Common year" << endl;
	}

	return 0;
}
\end{lstlisting}

\begin{tcolorbox}
	\mybox{运行结果}
	\begin{verbatim}
Enter a year: 2020
Leap year
\end{verbatim}
\end{tcolorbox}

\vspace{0.5cm}

\subsection{if-else if-else}

当需要对更多的条件进行判断时，可以使用if-else if-else语句。\\

\mybox{字符}

\begin{lstlisting}[language=C++]
#include <iostream>

using namespace std;

int main()
{
	char c;
	cout << "Enter a character: ";
	cin >> c;

	if(c >= 'a' && c <= 'z')
	{
		cout << "Lowercase" << endl;
	}
	else if(c >= 'A' && c <= 'Z')
	{
		cout << "Uppercase" << endl;
	}
	else if(c >= '0' && c <= '9')
	{
		cout << "Digit" << endl;
	}
	else
	{
		cout << "Special character" << endl;
	}
	
	return 0;
}
\end{lstlisting}

\begin{tcolorbox}
	\mybox{运行结果}
	\begin{verbatim}
Enter a character: T
Uppercase
\end{verbatim}
\end{tcolorbox}

\newpage

\section{switch}

\subsection{switch}

switch结构用于根据一个整数值，选择对应的case执行。需要注意的是，当对应的case中的代码被执行完后，并不会像if语句一样跳出switch结构，而是会继续向后执行，直到遇到break。\\

\mybox{计算器}

\begin{lstlisting}[language=C++]
#include <iostream>

using namespace std;

int main() {
	int num1, num2;
	char op;

	cout << "Enter an expression: ";
	cin >> num1 >> op >> num2;

	switch (op)
	{
	case '+':
		cout << num1 << " + " << num2 << " = " << num1 + num2 << endl;
		break;
	case '-':
		cout << num1 << " - " << num2 << " = " << num1 - num2 << endl;
		break;
	case '*':
		cout << num1 << " * " << num2 << " = " << num1 * num2 << endl;
		break;
	case '/':
		cout << num1 << " / " << num2 << " = " << num1 / num2 << endl;
		break;
	default:
		cout << "Error! Operator is not supported" << endl;
		break;
	}

	return 0;
}
\end{lstlisting}

\begin{tcolorbox}
	\mybox{运行结果}
	\begin{verbatim}
Enter an expression: 5 * 8
5 * 8 = 40
\end{verbatim}
\end{tcolorbox}

\newpage